import {
  BUTTON,
  DEMO,
  INIT_SYSTEM,
  LOAD,
  LOAD_EMPTY,
  MENU,
  NAVIGATION_BAR, SIDE_MENU_ACTIVATION,
  SIDE_MENU_SELECTED,
  USER
} from '@/store/methodConstant'
import axios from '@/network/network'

const system = {
  state:{
    load : 0,
    navigationBar:[],
    sideMenuSelected:null,
  },
  mutations: {
    [LOAD](state,data){
      state.load =  state.load + data
    },
    [LOAD_EMPTY](state){
      state.load = 0
    },
    [NAVIGATION_BAR](state,data){
      state.navigationBar = data
    },
    [SIDE_MENU_SELECTED](state,data){
      state.sideMenuSelected = data
    }
  },
  actions: {
    [INIT_SYSTEM](context){
      //获得菜单
      axios.get('/api/menu/loginMenu').then(res => {
          context.commit(MENU,res.data.data)
          context.commit(LOAD,1)
      }).catch(() =>  context.commit(LOAD,1))
      //获得按钮
      axios.get('/api/menu/tree/button').then(res => {
        context.commit(BUTTON,res.data.data)
        context.commit(LOAD,1)
      }).catch(() =>  context.commit(LOAD,1))
      //获得用户
      axios.get('/api/userDetails').then(res => {
        context.commit(USER,res.data.data)
        context.commit(LOAD,1)
      }).catch(() =>  context.commit(LOAD,1))
      //顶部菜单初始化
      context.commit(SIDE_MENU_ACTIVATION,'0')
    }
  },
}

export default system
